package com.ghh.app.photoframe;

import java.io.IOException;
import java.io.InputStream;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

public class PhotoFrameWidgetProvider extends AppWidgetProvider {
	private final static String	TAG	= "System.out";
	private final static long interval = 30000;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, String.format("Receive widget[%1$s] action[%2$s]", 
				intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID),
				intent.getAction()));
		
		String action = intent.getAction();
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		if (action.equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
			int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
			if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				PhotoFrame pf = new PhotoFrame(widgetId, context);
				if (pf.exist()) {
					PhotoFrameWidgetProvider.updateAppWidget(context, appWidgetManager, widgetId);
					pendingUpdate(context, widgetId);
				} else {
					Log.i(TAG, widgetId + " not exist");
				}
			}
		} else {
			super.onReceive(context, intent);
		}
	}

	private void pendingUpdate(Context context, int appWidgetId) {
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, PhotoFrameWidgetProvider.class);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
		PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
		alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + interval, pending);
	}

	private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
		PhotoFrame pf = new PhotoFrame(appWidgetId, context);
		String img = pf.getCurrentPhoto();
		if (img == null) {
			return;
		}

		pf.setCurrentIndex(pf.getCurrentIndex() + 1);
		pf.save();

		Log.i(TAG, "display image:" + img);
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.photoframe_appwidget);
		InputStream in = null;
		try {
			in = context.getContentResolver().openInputStream(Uri.parse(img));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Bitmap bitmap = BitmapFactory.decodeStream(in);
		views.setImageViewBitmap(R.id.widgetImage, bitmap);
		appWidgetManager.updateAppWidget(appWidgetId, views);
	}

	// private boolean instanceExists(Context context, AppWidgetManager
	// appWidgetManager, int widgetId) {
	// int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new
	// ComponentName(context, PhotoFrameWidgetProvider.class));
	// if (appWidgetIds == null || appWidgetIds.length == 0) {
	// return false;
	// }
	// for (int id : appWidgetIds) {
	// if (id == widgetId) {
	// return true;
	// }
	// }
	// return false;
	// }

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		for (int appWidgetId : appWidgetIds) {
			PhotoFrame pf = new PhotoFrame(appWidgetId, context);
			pf.remove();
		}
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		Log.i(TAG, "disable widget");
		// UpdateWidgetService.stopService(context);
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		Log.i(TAG, "enable widget");
		// UpdateWidgetService.startService(context);
		super.onEnabled(context);
	}

	@Override
	public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
		super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
	}
}
